<?php
class onermbController extends RPHP_Controller
{
    //一元拍.九云币
	public function pat_coins()
	{
		$m    = $this->model;
        $user = $m->checkAuth();
        //if ($user) {            
        $data              = $m->getPatCoins();
        $this->view->data  = $data; 
   
        $this->display();
	}
	
	//九云币明细
	public function coins_record()
	{
	    $m    = $this->model;
	    $user = $m->checkAuth();
	    if ($user) {
	        $seach = $_GET['seach']>0?$_GET['seach']:0;
    	    $data              = $m->getCoinsRecord($seach);
    	    $this->view->data  = $data;
    	    $this->display();
    	    
	    }
	    
	}
	
	//九云币充值
	public function coins_recharge()
	{
	    $m    = $this->model;
	    $user = $m->checkAuth();
	    if ($user) {
	        $this->display();
	    }
	    
	}
	
    public function pay_show()
    {
        $this->display();
        
    }
	//九云币充值
	public function pay_coins()
	{
	    $m    = $this->model;
	    $user = $m->checkAuth();
	    if ($user && $_POST['act'] == "coins_recharge") {
	       
	        $goto     = true;
	       
	        $data = $m->saveCoinsMoney($_POST);
	       
    	            if ($user['app'] == 1) {
        	            $transaction_type = 2;
        	        } else {
        	            $transaction_type = 1;
        	        }
        	        
        	        if ($data['code'] = 200 && $data['id']>0 && $data['total_money']>0) {
        	          
        	               
        	            if ($this->c['pay'][$transaction_type]) {
        	        
        	                $para['channel_id']     = $this->c['pay'][$transaction_type]['channel_id'];
        	                $para['out_trade_no']   = $data['id'];
        	                $para['out_trade_type'] = "COINS";
        	                $para['total_fee']      = $data['total_money'];
        	                $para['timestamp']      = $_SERVER['REQUEST_TIME'];
        	                ksort($para);
        	                foreach ($para as $key => $value) {
        	                    $tmp[] = $key . "=" . $value;
        	                }
        	                $stringSignTemp = implode("&", $tmp) . "&channer_key=" . $this->c['pay'][$transaction_type]['channer_key'];
        	                $para['sign']   = strtoupper(md5($stringSignTemp));
        	                $tmp[]          = "sign=" . $para['sign'];
        	                $goto           = false;
        	                $this->redirect($this->c['pay'][$transaction_type]['gate_url'] . "?" . implode("&", $tmp));
        	        
        	            }
        	        }
    	      
	       
	    
	    }
	    
	}
	
	//一元拍 拍中付款
	public function pat_sueecss_pay()
	{
	    $m    = $this->model;
	    $user = $m->checkAuth();
	    if ($user) {
	        $goto     = true;
	        $id = intval($_GET['id']);
	       
	        if ($user['app'] == 1) {
	            $transaction_type = 2;
	        } else {
	            $transaction_type = 1;
	        }
	         
	        if ($id > 0) {
	    
	              $data = $m->savePatOrder($id);
	            if ($this->c['pay'][$transaction_type]) {
	                 
	                $para['channel_id']     = $this->c['pay'][$transaction_type]['channel_id'];
	                $para['out_trade_type'] = "ORDER";
	                $para['total_fee']      = $money;
	                $para['timestamp']      = $_SERVER['REQUEST_TIME'];
	                ksort($para);
	                foreach ($para as $key => $value) {
	                    $tmp[] = $key . "=" . $value;
	                }
	                $stringSignTemp = implode("&", $tmp) . "&channer_key=" . $this->c['pay'][$transaction_type]['channer_key'];
	                $para['sign']   = strtoupper(md5($stringSignTemp));
	                $tmp[]          = "sign=" . $para['sign'];
	                $goto           = false;
	                $this->redirect($this->c['pay'][$transaction_type]['gate_url'] . "?" . implode("&", $tmp));
	                 
	            }
	        }
	    
	    }
	    
	}
	
	//补偿明细
	
	public function compen_record()
	{
	    $m    = $this->model;
	    $user = $m->checkAuth();
	    if ($user) {
	        $data              = $m->getCompenRecord();
	        $this->view->data  = $data;
	        $this->display();
	         
	    }
	    
	}
	
	//九云币提现表单
	public function pickup()
	{
	
	    $m    = $this->model;
	    $user = $m->checkAuth();
	    if ($user) {
	        $this->view->getSetting = $m ->getSetting();
	        $this->view->bank = $m->myBankList();
	        $this->view->balance = $m->getMemberMoney();
	        $this->view->page_title = "九云币提现";
	        $this->display();
	         
	    }
	}
	//提现操作
	public function ajax_pickup()
	{
	    $m    = $this->model;
	    $user = $m->checkAuth(false);
	    if ($user) {
	        $res['code'] = 800;
	        if ($user['uid'] > 0) {
	             
	            $res = $m->doWithdraw($_POST);
	
	        }
	
	        echo exit(json_encode($res));
	    }
	}
	//一元拍详情
	public function one_pat_detail()
	{
	    $m    = $this->model;
	    $user = $m->checkAuth();
	  
	    if ($user) {
	        $goods_id = intval($_GET['goods_id']);
	        $data              = $m->getOnePatGoods($goods_id);
	        
	        if($data){
	            $data['time'] =time(); 
	            //echo $data['pat_count_down'];die; 
	            //print_r(date('H:i:s',$data['pat_count_time']));die;
	           $pats = $m->getPatRecord($goods_id);
	           $config = $m->getSetting();
	          
	           $this->view->config= $config;
	           $this->view->pats  = $pats;
    	       $this->view->data  = $data;
    	       $this->display();
	       
	        }
	    }
	}
	

	
	//一元拍记录
	public function pat_records()
	{
	    $m    = $this->model;
	    $user = $m->checkAuth();
	    if ($user) {
	        $goods_id = intval($_GET['goods_id']);
	      
	        if($goods_id){
	            $data = $m->getPatRecords($goods_id);
	           
	            $this->view->time  = time();
	            $this->view->data  = $data;
	    	    $this->display();
	
	        }
	    }
	}
	//定时
	public function save_pat_status(){
	 
	    //定时
	    $m    = $this->model;
	    $data = $m->savePatStatus();
	    echo $data;die;
	}
	//定时跟拍
	public function following()
	{
	    
	    $m    = $this->model;
	    $data = $m->saveFollowing();
	    echo $data;die;
	}
	
	public function ajax()
	{
	    $m    = $this->model;
	    $user = $m->checkAuth();
	    if ($user) {
    	    if($_POST['act']=="pat_buy"){
    	        //print_r($_POST);die;
    	        $data = $m->savePatBuy($_POST);
    	       
    	        
    	    }
    /* 	    elseif($_POST['act']=="save_pat"){
    	        //定时
    	        $data = $m->savePatStatus($_POST);
    	       
    	    } */
    	    elseif($_POST['act']=="uncloud"){
    	        //解冻云米
    	        $data = $m->saveUncloud($_POST);
    	       
    	    }
    	    
    	   
    	    
    	    echo json_encode($data);
	    
	    }
	}
	
	//我的拍中
	public function pat_success()
	{
	    $m    = $this->model;
	    $user = $m->checkAuth();
	    if ($user) {
	        $data = $m->getPatList();
	        $this->view->data  = $data['pat_success'];
	        
	        $this->view->time  = time();
	        $this->display();
	    }
	    
	}
	//超时弃权
	public function past_time()
	{
	    $m    = $this->model;
	    $user = $m->checkAuth();
	    if ($user) {
	        $data = $m->getPatList();
	        $this->view->data  = $data['pat_past_time'];
	        
	        $this->display();
	    }
	    
	}
	
	//正在拍
	public function pat_being()
	{
	    $m    = $this->model;
	    $user = $m->checkAuth();
	    if ($user) {
	        
	        $data = $m->getPatBeging();
	        $this->view->time  = time();
	        $this->view->data  = $data['pat_being'];
	        $this->display();
	    }
	}
	
	//结算页面
	public function checkout()
	{
	    
	    $goto = true;
	    
	    $m    = $this->model;
	    $user = $m->checkAuth();
	    if ($user) {
	        $patid = intval($_GET['id']);  
           
	        $data = $m->getPatDetail($patid);
	       // print_r($data);die;
	        if ($data['pay_down_time']>time()) {
	          
	            $member_data = $m->getMemberInfo();
	    
	            //判断是否是会员或已经到期，会员免运费
	            if ($member_data['member_level'] >= 1 && $member_data['level_past_time'] > $_SERVER['REQUEST_TIME']) {
	                $member_level = 1;
	                $shipping_fee = 0;
	    
	            } else {
	                $member_level = 0;
	                $shipping_fee = 8;
	            }
	    
	            $this->view->member_level = $member_level;
	            $is_free_shipping         = 0;
	            if ($data['is_free_shipping']) {
	                $is_free_shipping = $data['is_free_shipping'];
	                $shipping_fee     = 8;
	            }
	            $this->view->data             = $data;
	            $this->view->is_free_shipping = $is_free_shipping;
	            $total_price =$data['bid_price'];
	          
	            //计算运费
	            $total_price += $shipping_fee;
	    
	            $addressid = 0;
	            if ($_GET['addressid'] != "") {
	                $addressid = intval($_GET['addressid']);
	            }
	    
	            //获取收获地址信息
	            $this->view->address = $m->getAddress($user['uid'], $addressid, true);
	   
	            if ($data['bid_price'] > 0) {
	    
	                $this->view->goods_amount = number_format($total_price, 2, ".", "");
	                $this->view->total_price  = number_format($total_price, 2, ".", "");
	                $this->view->province     = $m->getRegion();
	                $this->view->shipping_fee = $shipping_fee;
	    
	                $this->view->coupon = $m->getCoupon();
	    
	                $goto = false;
	                $this->display();
	    
	            }
	    
	        }
	    
	    }
	    
	    if ($goto) {
	        $this->redirect("/onermb/pat_success");
	    }
	}
	
	//去付款
	public function pay()
	{
	    $m    = $this->model;
	    $user = $m->checkAuth();
	    if ($user['uid'] > 0 && $_POST['act'] == "save" && $_POST['id']) {
	        $patDetail = $m->getPatDetail($_POST['id']);
	        
	       if($patDetail['order_id']>0){
	            
	            
	           $this->redirect("/order/order?type=1&is_pat=1");
	        }else{
	                $data = $m->saveOrder($_POST,$patDetail);
	        }
	        if ($user['app'] == 1) {
	            $data['transaction_type'] = 2;
	        } else {
	            $data['transaction_type'] = 1;
	        }
	         
	       
	        if ($data['code'] == 200 && $data['order_sn'] != "" && $data['pay_total_fee'] > 0 && $data['order_id'] > 0 && $this->c['pay'][$data['transaction_type']]) {
	             
	            $para['channel_id']     = $this->c['pay'][$data['transaction_type']]['channel_id'];
	            $para['out_trade_no']   = $data['order_id'];
	            $para['out_trade_type'] = "ORDERPAT";
	            $para['total_fee']      = $data['pay_total_fee'];
	            $para['timestamp']      = $_SERVER['REQUEST_TIME'];
	            ksort($para);
	            foreach ($para as $key => $value) {
	                $tmp[] = $key . "=" . $value;
	            }
	            $stringSignTemp = implode("&", $tmp) . "&channer_key=" . $this->c['pay'][$data['transaction_type']]['channer_key'];
	            $para['sign']   = strtoupper(md5($stringSignTemp));
	            $tmp[]          = "sign=" . $para['sign'];
	             
	            $this->redirect($this->c['pay'][$data['transaction_type']]['gate_url'] . "?" . implode("&", $tmp));
	             
	        } else {
	             
	            $this->redirect("/cart/failed?code=" . $data['code']);
	             
	        }
	         
	    }
	     
	}
	
	public function service()
	{
	    $m    = $this->model;
	    $user = $m->checkAuth();
	    if ($user) {
	        $data =  $m->getService();
	       
	        $this->view->data  = $data;
	        
	        $this->display();
	    }
	    
	}
	//云米解冻申请
	public function uncloud()
	{
	    $m    = $this->model;
	    $user = $m->checkAuth();
	    if ($user) {
	        $data = $m->getUncolud();
	       
	        $this->view->data  = $data;
	        $this->display();
	    }
	    
	}
	
}